package com.panghl.report;

import com.microsoft.schemas.office.visio.x2012.main.impl.CellTypeImpl;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

/**
 * @Author panghl
 * @Date 2021/4/21 21:16
 * @Description 读Excel
 **/
public class ExcelRead {

    public static void read03() throws Exception {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream("03.xls");
        //1.创建一个工作簿  使用excel能操作的这边他都可以操作！
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        //2.得到表
        Sheet sheet = workbook.getSheetAt(0);
        //3.得到行
        Row row = sheet.getRow(1);
        //4.得到列
        Cell cell = row.getCell(0);
        //读取值一定要注意类型！！！
        //getStringCellValue 字符串类型
//        System.out.println(cell.getStringCellValue());
        System.out.println(cell.getNumericCellValue());
        fileInputStream.close();
    }

    public static void read07() throws Exception {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream("07.xlsx");
        //1.创建一个工作簿  使用excel能操作的这边他都可以操作！
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        //2.得到表
        Sheet sheet = workbook.getSheetAt(0);
        //3.得到行
        Row row = sheet.getRow(1);
        //4.得到列
        Cell cell = row.getCell(0);
        //读取值一定要注意类型！！！
        //getStringCellValue 字符串类型
//        System.out.println(cell.getStringCellValue());
        System.out.println(cell.getNumericCellValue());
        fileInputStream.close();
    }


    public static void cellType() throws IOException {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream("明细表.xlsx");
        //1.创建一个工作簿  使用excel能操作的这边他都可以操作！
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        //获取标题内容
        Row rowTitle = sheet.getRow(0);
        if (rowTitle != null) {
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                Cell cell = rowTitle.getCell(cellNum);
                if (cell != null) {
                    String stringCellValue = cell.getStringCellValue();
                    System.out.print(stringCellValue + "|");
                }
            }
            System.out.println();
        }
        //获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 0; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            if (rowData != null) {
                //读取列
                int cellCount = rowData.getPhysicalNumberOfCells();
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    System.out.print("[" + rowNum + "-" + cellNum + "]");
                    Cell cell = rowData.getCell(cellNum);
                    //匹配列的数据类型
                    if (cell != null) {
                        CellType cellType = cell.getCellType();
                        String cellValue = "";
                        switch (cellType) {
                            case STRING: //字符串
                                cellValue = cell.getStringCellValue();
                                break;
                            case BOOLEAN: //布尔
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                break;
                            case BLANK: //空
                                System.out.println("空");
                                break;
                            case NUMERIC: //数字(日期、普通数字)
                                if (HSSFDateUtil.isCellDateFormatted(cell)){
                                    System.out.print("日期");
                                    Date date = cell.getDateCellValue();
                                    cellValue=new DateTime(date).toString("yyyy-MM-dd");
                                }else {
                                    //不是日期格式，防止数字过长！
                                    System.out.print("转换为字符串输出");
                                    cell.setCellType(CellType.STRING);
                                    cellValue = cell.getStringCellValue();
                                }
                                break;
                            case ERROR: //错误
                                System.out.print("数据类型错误");
                                break;
                        }
                        System.out.println(cellValue);
                    }
                }
                System.out.println();
            }
        }
        fileInputStream.close();
    }

    //公式
    public static void formula() throws Exception{
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream("公式.xlsx");
        //1.创建一个工作簿  使用excel能操作的这边他都可以操作！
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);

        Row row = sheet.getRow(4);
        Cell cell = row.getCell(0);

        //拿到计算公式 eval
        XSSFFormulaEvaluator xssfFormulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);

        //输出单元格的内容
        CellType cellType = cell.getCellType();
        switch (cellType){
            case FORMULA:
                String formula = cell.getCellFormula();
                System.out.println(formula);
                //计算
                CellValue evaluate = xssfFormulaEvaluator.evaluate(cell);
                String cellValue = evaluate.formatAsString();
                System.out.println(cellValue);
                break;
        }

    }

    public static void main(String[] args) throws Exception {
        formula();
    }
}
